package au.com.bus4u.service.impl;

import au.com.bus4u.dao.DaoSupport;
import au.com.bus4u.exception.ServiceException;
import au.com.bus4u.service.OrderOldService;
import au.com.bus4u.utils.DateUtil;
import au.com.bus4u.utils.DynamicTask;
import au.com.bus4u.utils.PageData;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.concurrent.ScheduledFuture;

@Service("orderOldService")
public class OrderOldServiceImpl implements OrderOldService {

    @Autowired
    private DaoSupport daoSupport;

    private String mapper = "OrderOldMapper.";


    @Override
    public int save(PageData pd) throws Exception {
        //游客新增订单非空验证
//        checkUserInfo(pd);

        //新增订单填入默认值
//        pd = addOrderDefaultValue(pd);
//
//        if(pd.get("stop[0][stop]")!=null && !"".equals(pd.get("stop[0][stop]"))){
//            pd.put("stop1",pd.get("stop[0][stop]"));
//        }
//        if(pd.get("stop[1][stop]")!=null && !"".equals(pd.get("stop[1][stop]"))){
//            pd.put("stop2",pd.get("stop[1][stop]"));
//        }
//        if(pd.get("stop[2][stop]")!=null && !"".equals(pd.get("stop[2][stop]"))){
//            pd.put("stop3",pd.get("stop[2][stop]"));
//        }
//        if(pd.get("stop[3][stop]")!=null && !"".equals(pd.get("stop[3][stop]"))){
//            pd.put("stop4",pd.get("stop[3][stop]"));
//        }
//        if(pd.get("stop[4][stop]")!=null && !"".equals(pd.get("stop[4][stop]"))){
//            pd.put("stop5",pd.get("stop[4][stop]"));
//        }
        return (int) daoSupport.save(mapper+"save", pd);
    }


    @Override
    @Transactional
    public int edit(PageData pd) throws Exception {

        //检查修改数据
        pd = checkUpdata(pd);

        return (int) daoSupport.update(mapper+"edit", pd);
    }



    @Override
    public int delete(Integer idorders) throws Exception {

        PageData pd = new PageData();
        pd.put("idorders",idorders);

        return (int) daoSupport.delete(mapper+"deleteById", pd);
    }

    @Override
    public int deleteAll(String[] DATA_IDS) throws Exception {
        return (int)daoSupport.batchDelete(mapper+"deleteAll", DATA_IDS);
    }



    @Override
    public List<PageData> list(PageData pd) throws Exception {
        //判断时间范围的搜索条件
//        System.err.println("OrderServiceImpl.list");

        if(pd.get("timeexact_select") != null && !"".equals(pd.get("timeexact_select"))){
            String timeexact_select = pd.get("timeexact_select").toString();
            String[] timeexact_split = timeexact_select.split(" - ");
            Date timeexact_start = DateUtil.formatString(timeexact_split[0], "yyyy-MM-dd HH:mm:ss");
            Date timeexact_end = DateUtil.formatString(timeexact_split[1], "yyyy-MM-dd HH:mm:ss");
            pd.put("timeexact_start",timeexact_start);
            pd.put("timeexact_end",timeexact_end);

        }
        //order_status订单状态
        if(pd.get("contact") == null || "".equals(pd.get("contact"))){
            pd.remove("contact");
        }
        if(pd.get("driver_id") == null || "".equals(pd.get("driver_id"))){
            pd.remove("driver_id");
        }
        if(pd.get("custo_id") == null || "".equals(pd.get("custo_id"))){
            pd.remove("custo_id");
        }
        if(pd.get("op_id") == null || "".equals(pd.get("op_id"))){
            pd.remove("op_id");
        }
        if(pd.get("email") == null || "".equals(pd.get("email"))){
            pd.remove("email");
        }
        if(pd.get("fromwhere") == null || "".equals(pd.get("fromwhere"))){
            pd.remove("fromwhere");
        }
        if(pd.get("towhere") == null || "".equals(pd.get("towhere"))){
            pd.remove("towhere");
        }
        if(pd.get("order_type") == null || "".equals(pd.get("order_type"))){
            pd.remove("order_type");
        }
        if(pd.get("order_status") == null || "".equals(pd.get("order_status"))){
            pd.remove("order_status");
        }
        if(pd.get("time_needed") == null || "".equals(pd.get("time_needed"))){
            pd.remove("time_needed");
        }
        if(pd.get("custo_sum") == null || "".equals(pd.get("custo_sum"))){
            pd.remove("custo_sum");
        }
        if(pd.get("distance_s") == null || "".equals(pd.get("distance_s"))){
            pd.remove("distance_s");
        }
        if(pd.get("distance_l") == null || "".equals(pd.get("distance_l"))){
            pd.remove("distance_l");
        }
        if(pd.get("custo_costs") == null || "".equals(pd.get("custo_costs"))){
            pd.remove("custo_costs");
        }
        if(pd.get("flight_id") == null || "".equals(pd.get("flight_id"))){
            pd.remove("flight_id");
        }
        if(pd.get("driver_order_type") == null || "".equals(pd.get("driver_order_type"))){
            pd.remove("driver_order_type");
        }
        if(pd.get("order_source") == null || "".equals(pd.get("order_source"))){
            pd.remove("order_source");
        }
        if(pd.get("comment") == null || "".equals(pd.get("comment"))){
            pd.remove("comment");
        }
        if(pd.get("email") == null || "".equals(pd.get("email"))){
            pd.remove("email");
        }
        if(pd.get("timeexact_select") == null || "".equals(pd.get("timeexact_select"))){
            pd.remove("timeexact_select");
        }

        List<PageData> list = (List<PageData>) daoSupport.findForList(mapper+"list",pd);
        return list;
    }

    @Override
    public PageData findById(Integer idorders) throws Exception {
        PageData pd = new PageData();
        pd.put("idorders",idorders);
        PageData pdObj = (PageData) daoSupport.findForObject(mapper+"findById", pd);
        return pdObj;
    }


    /**
     * 通过用户id和 开始时间 与 结束时间的搜索条件查询，查询当前用户的订单列表
     * @param
     * @return
     * @throws Exception
     */
    @Override
    public List<PageData> findByUserId(String custo_id,String startTime,String endTime,Integer order_status) throws Exception {
        PageData pd = new PageData();
        pd.put("custo_id",custo_id);
//        if(startTime != null && !"".equals(startTime)){
//            Date start = DateUtil.formatString(startTime, "yyyy-MM-dd HH:mm:ss");
//            pd.put("start",start);
//        }
//        if(endTime != null && !"".equals(endTime)){
//            Date end = DateUtil.formatString(endTime, "yyyy-MM-dd HH:mm:ss");
//            pd.put("end",end);
//        }
//        if(order_status != null ){
//            pd.put("order_status",order_status);
//        }
        List<PageData> list  = (List<PageData>) daoSupport.findForList(mapper+"findByStarAndEndTime",pd);
//
        return list;
    }






    /**
     * 通过订单id获取该订单的状态order_status
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public Integer findStatusById(Integer id) throws Exception {
        return findStatusByOrderId(id);
    }
    
    private Integer findStatusByOrderId(Integer idorders) throws Exception {
        PageData pageData = new PageData();
        pageData.put("idorders",idorders);
        int order_status = (int)daoSupport.findForObject(mapper+"findStatusById",pageData);
        return order_status;
    }


    /**
     * 验证订单必填项数据 未使用
     * @param pd
     */
    private void checkUserInfo(PageData pd){
        if( pd.get("passengers") ==null ||"".equals(pd.get("passengers").toString())){
            throw new ServiceException("订单创建失败:订单游客人数passengers不能空");
        }

        if(pd.get("big_luggage") ==null|| "".equals(pd.get("big_luggage").toString()) ){
//            throw new ServiceException("订单创建失败:大件行李数量big_luggage不能空");
            pd.put("big_luggage",0);
        }


        if(pd.get("order_type") ==null|| "".equals(pd.get("order_type").toString()) ){
            throw new ServiceException("订单创建失败:订单类型order_type不能空");
        }
    }
    /**
     * 检查修改数据
     * @param pd
     */
    private PageData checkUpdata(PageData pd){
        if( pd.get("idorders") ==null ||"".equals(pd.get("idorders").toString())){
            throw new ServiceException("旧订单更新失败:idorders不能空");
        }

        if(pd.get("contact") ==null|| "".equals(pd.get("contact").toString()) ){
            pd.remove("contact");
        }
        if(pd.get("driver_id") ==null|| "".equals(pd.get("driver_id").toString()) ){
            pd.remove("driver_id");
        }
        if(pd.get("custo_id") ==null|| "".equals(pd.get("custo_id").toString()) ){
            pd.remove("custo_id");
        }
        if(pd.get("op_id") ==null|| "".equals(pd.get("op_id").toString()) ){
            pd.remove("op_id");
        }
        if(pd.get("email") ==null|| "".equals(pd.get("email").toString()) ){
            pd.remove("email");
        }
        if(pd.get("fromwhere") ==null|| "".equals(pd.get("fromwhere").toString()) ){
            pd.remove("fromwhere");
        }
        if(pd.get("towhere") ==null|| "".equals(pd.get("towhere").toString()) ){
            pd.remove("towhere");
        }
        if(pd.get("order_type") ==null|| "".equals(pd.get("order_type").toString()) ){
            pd.remove("order_type");
        }
        if(pd.get("order_status") ==null|| "".equals(pd.get("order_status").toString()) ){
            pd.remove("order_status");
        }
        if(pd.get("time_needed") ==null|| "".equals(pd.get("time_needed").toString()) ){
            pd.remove("time_needed");
        }
        if(pd.get("custo_sum") ==null|| "".equals(pd.get("custo_sum").toString()) ){
            pd.remove("custo_sum");
        }
        if(pd.get("distance_s") ==null|| "".equals(pd.get("distance_s").toString()) ){
            pd.remove("distance_s");
        }
        if(pd.get("distance_l") ==null|| "".equals(pd.get("distance_l").toString()) ){
            pd.remove("distance_l");
        }
        if(pd.get("custo_costs") ==null|| "".equals(pd.get("custo_costs").toString()) ){
            pd.remove("custo_costs");
        }
        if(pd.get("flight_id") ==null|| "".equals(pd.get("flight_id").toString()) ){
            pd.remove("flight_id");
        }
        if(pd.get("driver_order_type") ==null|| "".equals(pd.get("driver_order_type").toString()) ){
            pd.remove("driver_order_type");
        }
        if(pd.get("order_source") ==null|| "".equals(pd.get("order_source").toString()) ){
            pd.remove("order_source");
        }
        if(pd.get("comment") ==null|| "".equals(pd.get("comment").toString()) ){
            pd.remove("comment");
        }
        if(pd.get("timeexact") ==null|| "".equals(pd.get("timeexact").toString()) ){
            pd.remove("timeexact");
        }

        return pd;
    }



    /**
     * 新增订单填入默认值 未使用
     * @param pd
     * @return
     */
    private PageData addOrderDefaultValue(PageData pd) {
        //获取当前登录用户数据
        PageData currentUser = getCurrentUser();

//        pd.put("order_id", UuidUtil.get32UUID());
        pd.put("order_prefix_name","bus4u#");
        pd.put("user_id",getCurrentUserID());
        pd.put("order_status",0);
        pd.put("order_status_name","Waiting");
        pd.put("contact_name",currentUser.get("avatar"));
        pd.put("contact_phone",currentUser.get("phone"));
        pd.put("contact_email",currentUser.get("user_email"));
        pd.put("add_time",new Date());

        return pd;
    }

    /**
     * 获取当前登录用户数据
     * @return
     */
    private PageData getCurrentUser() {
        return (PageData) SecurityUtils.getSubject().getSession().getAttribute("currentUser");
    }
    private String getCurrentUserID(){
        PageData pd = (PageData) SecurityUtils.getSubject().getSession().getAttribute("currentUser");
        String user_id = pd.get("user_id").toString();
        return user_id;
    }

}
